
# Import what's needed 
from sqlobject import SQLObject, \
                      IntCol, \
                      MultipleJoin, \
                      StringCol, \
                      DateTimeCol, \
                      BLOBCol, \
                      FloatCol, \
                      ForeignKey






# A simple member class
class userTable( SQLObject ):
  username = StringCol( unique = True )
  challenges = MultipleJoin( 'challengeTable' )






class challengeTable( SQLObject ):
  # Challenge stuff
  challengeID = IntCol( alternateID = True )
  challengeURL = StringCol()
  challengeName = StringCol()

  # The entry
  entryID = IntCol( alternateID = True )
  entryURL = StringCol()
  entryName = StringCol()
  entryImageURL = StringCol()
  entryThumbURL = StringCol()

  # The images
  entryImage = BLOBCol()
  entryThumb = BLOBCol()

  # Time/date
  timeStamp = DateTimeCol( default = DateTimeCol.now )

  # Relations from this
  user = ForeignKey( 'userTable' )
  votes = MultipleJoin( 'votesTable' )







class voteVal():
  votes = int()
  views = int()
  score = float()
  comments = int()
  favourites = int()
  wishList = int()
  vote = None



  def __getitem__( self, s ):
    if s is "votes":
      return self.votes
    elif s is "views":
      return self.views
    elif s is "score":
      return self.score
    elif s is "comments":
      return self.comments
    elif s is "favourites":
      return self.favourites
    elif s is "wishList":
      return self.wishList
    return None


  def __init__( self,
                votes = int(),
                views = int(),
                score = float(),
                comments = int(),
                favourites = int(),
                wishList = int(),
                voteEntry = None,
                vote = None ):
    if voteEntry is not None:
      self.votes = int( voteEntry.votes )
      self.views = int( voteEntry.views )
      self.score = voteEntry.score
      self.comments = int( voteEntry.comments )
      self.favourites = int( voteEntry.favourites )
      self.wishList = int( voteEntry.wishList )


    else:
      self.votes = int( votes )
      self.views = int( views )
      self.score = score
      self.comments = int( comments )
      self.favourites = int( favourites )
      self.wishList = int( wishList )
    self.vote = vote



class voteTable( SQLObject ):
  challenge = ForeignKey( 'challengeTable' )

  # Current status of the image
  votes = IntCol()
  views = IntCol()
  score = FloatCol()
  comments = IntCol()
  favourites = IntCol()
  wishList = IntCol()

  # Time stamp
  timeStamp = DateTimeCol( default = DateTimeCol.now )


  @staticmethod
  def getColumns( writable = False ):
    if writable is True:
      return ["Votes", "Views", "Score", "Comments", "Favourites", "Wish list"]

    return ["votes", "views", "score", "comments", "favourites", "wishList"]

  def __getitem__( self, s ):
    if s is "votes":
      return self.votes
    elif s is "views":
      return self.views
    elif s is "score":
      return self.score
    elif s is "comments":
      return self.comments
    elif s is "favourites":
      return self.favourites
    elif s is "wishList":
      return self.wishList
    return None


  # Loader helpef function
  @staticmethod
  def conv( score ):
    return voteVal( votes = score.votes,
                     views = score.views,
                     score = score.score,
                     comments = score.comments,
                     favourites = score.favourites,
                     wishList = score.wishList )
